/*
 * Copyright 2013, NuzoweR@Schneefeuer <nuzower@gmail.com>.
 * The Settlers Online - Combat Simulator (tsosim).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Utils.h"
#include <stdexcept>

int utils::UTF8LEN(std::string const& str)
{
	// http://stackoverflow.com/questions/4063146/getting-the-actual-length-of-a-utf-8-encoded-stdstring

	const char* s = str.c_str();
	int len = 0;
	while (*s) len += (*s++ & 0xc0) != 0x80;

	return len;
}

bool utils::compare_string_prefix(std::string const& s1, std::string const& s2)
{
	typedef std::string::const_iterator iter_t;
	iter_t c1 = s1.begin();
	iter_t c2 = s2.begin();

	iter_t c1_end = s1.end();
	iter_t c2_end = s2.end();

	while(c1 != c1_end && c2 != c2_end) {
		if      ( *c1 < *c2 ) return true;
		else if ( *c1 > *c2 ) return false;
		++c1;
		++c2;
	}

	if(c1 == c1_end && c2 == c2_end) {
		// strings are equal
		return false; // or better throw now?? -> probably not
	}

	if(c1 != c1_end || c2 != c2_end) {
		// one string is prefix of the other
		return false;
		throw std::runtime_error("ambiguous prefix for : " + s1 + ", " + s2);
	}

	throw std::runtime_error("unexpected error for : " + s1 + ", " + s2);
}



void utils::trim_string(std::string& str)
{
	Pos_t first = str.find_first_not_of(" \t");
	Pos_t last  = str.find_last_not_of (" \t");

	if(first == std::string::npos) {
		str = "";
		return; // empty string
	}

	if(first != 0 || last != (str.length()-1))
	{
		str = str.substr( first, last-first+1 );
	}
	// else string already without leading and trailing whitespace
}


bool utils::extract_path_string(std::string const& str, int start_pos, std::string& path)
{
	Pos_t path_start = str.find_first_of("\"", start_pos);
	if(path_start == std::string::npos) 
	{
		return false;
	}

	path_start += 1;
	Pos_t path_end = str.find_first_of("\"", path_start);
	if(path_end == std::string::npos) 
	{
		return false;
	}

	path = str.substr(path_start, path_end-path_start);

	return true;
}

void utils::path_unify_separator(std::string& path, char sep)
{
	char new_sep = sep;
	char old_sep = sep == '/' ? '\\' : '/';

	std::string::iterator it = path.begin();
	for(; it != path.end(); ++it)
		if(*it == old_sep)
			*it = new_sep;
}

bool utils::path_get_dir_file(std::string const& path, std::string& dir, std::string& file)
{
	std::string my_path = path;
	path_unify_separator(my_path, '/');

	utils::Pos_t pos = my_path.find_last_of('/');
	if(pos == std::string::npos)
	{
		dir  = "";
		file = my_path;
	}
	else
	{
		dir  = my_path.substr(0, pos);
		file = my_path.substr(pos+1, my_path.length()-pos-1);
	}

	return true;
}



bool utils::process_line(std::string const& LINE, int start_pos, std::string& front, std::string& back)
{
	std::wstring trt = L"hello world";

	Pos_t col_pos = LINE.find_first_of(':', start_pos);
	if(col_pos == std::string::npos)
		return false;

	front = LINE.substr(start_pos, col_pos - start_pos);
	back  = LINE.substr(col_pos+1, LINE.length() - col_pos );

	trim_string(front);
	trim_string(back);

	return true;
}

bool utils::check_utf8_bom(std::string& line, bool remove_bom)
{
	if(line.length() >= 3)
	{
		const unsigned char bom[3] = {0xef, 0xbb, 0xbf};
		if( (unsigned char)line[0] == bom[0] &&
			(unsigned char)line[1] == bom[1] &&
			(unsigned char)line[2] == bom[2] )
		{
			if(remove_bom) {
				line = line.substr(3, line.length()-3);
			}
			return true;
		}
	}

	// no bom
	return false;
}

